package com.qbw.app.module.common.fragment;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.View;
import com.qbw.app.R;
import com.qbw.app.databinding.CommonFragmentLoopItemSelectTimeBinding;
import com.qbw.core.base.BaseDialogFragment;

import java.util.ArrayList;

/**
 * @author xiansx
 * @date 2020-10-12
 */
public class LoopItemSelectTimeDialogFragment extends BaseDialogFragment<CommonFragmentLoopItemSelectTimeBinding> implements View.OnClickListener {

    public static final String KEY_INIT_POS = "key_init_pos";
    public static final String KEY_STRING_ITEMS = "key_string_items";
    public static final String KEY_TYPE = "key_type";

    private int mInitPos;
    private ArrayList<String> mItems;

    private Object mListener;

    public static LoopItemSelectTimeDialogFragment createInstanceForTimeSelect(int item) {
        ArrayList<String> times = new ArrayList<>();
        int defPos = 0;
        for(int i = 1; i <= 24; i++) {
            if(item == i) {
                defPos = i - 1;
            }
            times.add(i + "h");
        }
        return createInstance(times, defPos);
    }

    public static LoopItemSelectTimeDialogFragment createInstance(ArrayList<String> items, int initPos) {
        Bundle args = new Bundle();
        args.putStringArrayList(KEY_STRING_ITEMS, items);
        args.putInt(KEY_INIT_POS, initPos);
        LoopItemSelectTimeDialogFragment fragment = new LoopItemSelectTimeDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int onGetLayoutId() {
        return R.layout.common_fragment_loop_item_select_time;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getArguments() : savedInstanceState;
        mItems = args.getStringArrayList(KEY_STRING_ITEMS);
        mInitPos = args.getInt(KEY_INIT_POS, 0);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding.lv.setNotLoop();
        mBinding.lv.setItems(mItems);
        mBinding.lv.setInitPosition(mInitPos);
        mBinding.tvSure.setOnClickListener(this);
        mBinding.tvCancel.setOnClickListener(this);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putStringArrayList(KEY_STRING_ITEMS, mItems);
        outState.putInt(KEY_INIT_POS, mInitPos);
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.tv_sure) {
            dismiss();
            if (mListener instanceof Listener) {
                ((Listener) mListener).onSelectItem(mItems.get(mBinding.lv.getSelectedItem()));
            }
        } else if (vid == R.id.tv_cancel) {
            dismiss();
        }
    }

    public LoopItemSelectTimeDialogFragment setListener(Listener listener) {
        mListener = listener;
        return this;
    }

    public interface Listener {
        void onSelectItem(String selectItem);
    }

}
